package com.hlx.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlx.entity.admin.Price;
import com.hlx.entity.admin.Train;
import com.hlx.service.admin.PriceService;
import com.hlx.service.admin.TrainService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/price")
public class PriceController {
    @Autowired
    private PriceService priceService;

    @GetMapping("/getAllPriceByPage/{pageSize}/{currentPage}")
    public Result<Page> getAllPriceByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage){
        //        分页构造
        Page page = new Page(currentPage,pageSize);
        return Result.success(priceService.page(page));
    }

    @GetMapping("/getPriceByPage/{pageSize}/{currentPage}/{search}")
    public Result<Page> getPriceByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage,@PathVariable String search){
        //        分页构造
        LambdaQueryWrapper<Price> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Price::getPriceType,search);
        Page page = new Page(currentPage,pageSize);
        return Result.success(priceService.page(page,queryWrapper));
    }

    @PostMapping("/addPrice")
    public Result<String> addPrice(@RequestBody Price price){
        if(priceService.save(price)){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping("/putPrice")
    public Result<String> putPrice(@RequestBody Price price){
        if(priceService.updateById(price)){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @DeleteMapping ("/deletePrice/{id}")
    public Result<String> deletePrice(@PathVariable Integer id){
        if(priceService.removeById(id)){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
